package com.cpi.cornpp.plants.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.plants.dto.PlantDTO;
import com.cpi.cornpp.plants.view.PlantView;

/**
 * Plant Converter
 * @author Edgar Joao
 *
 */
public class PlantConverter {

	/**
	 * Plant Converter View to DTO
	 * @param view
	 * @return
	 */
	public PlantDTO converterPlantViewToDTO(PlantView view){
		PlantDTO plantDTO = null;
		plantDTO = new PlantDTO();
		plantDTO.setPlantId(view.getPlantId());
		plantDTO.setShortName(view.getShortName());
		plantDTO.setDescription(view.getDescription());
		plantDTO.setStorageAmount(view.getStorageAmount());
		plantDTO.setUnloadAmount(view.getUnloadAmount());
		plantDTO.setGrindProtectionDays(view.getGrindProtectionDays());
		plantDTO.setPriority(view.getPriority());
		return plantDTO;
	}
	
	
	/**
	 * Plant Converter DTO to View
	 * @param dto
	 * @return
	 */
	public PlantView converterPlantDtoToView(PlantDTO dto){
		PlantView plantView = null;
		if(dto!=null){
			plantView = new PlantView();
			plantView.setPlantId(dto.getPlantId());
			plantView.setShortName(dto.getShortName());
			plantView.setDescription(dto.getDescription());
			plantView.setStorageAmount(dto.getStorageAmount());
			plantView.setUnloadAmount(dto.getUnloadAmount());
			plantView.setGrindProtectionDays(dto.getGrindProtectionDays());	
			plantView.setPriority(dto.getPriority());
		}
		return plantView;
	}
	
	/**
	 * Plant Converter List View to DTO 
	 * @param plantDTOs
	 * @return
	 */
	public List convertViews(List plantDTOs){
		List plantsDTOs = null;
		plantsDTOs = new ArrayList(0);
		
		if(!CollectionUtils.isEmpty(plantDTOs)){
			Iterator iterator = plantDTOs.iterator();
			PlantView e = null;
			while(iterator.hasNext()){
				e = (PlantView)iterator.next();
				plantsDTOs.add(converterPlantViewToDTO(e));
			}
		}
		return plantsDTOs;
	}
	
	/**
	 * Plant Converter List DTO to View
	 * @param plantDTOs
	 * @return
	 */
	public List convertDTOs (List plantDTOs){
		List plantsViews = null;
		plantsViews = new ArrayList(0);
		
		if(!CollectionUtils.isEmpty(plantDTOs)){
			Iterator iterator = plantDTOs.iterator();
			PlantDTO e = null;
			while(iterator.hasNext()){
				e = (PlantDTO)iterator.next();
				plantsViews.add(converterPlantDtoToView(e));
			}
		}
		return plantsViews;
	}
}
